import { Keyboard } from "../base_frame/Keyboard";
import { CameraEmulator } from "./CameraEmulator";
/**
 * 通过设置正射投影 调整显示窗口的内容
 * near 调整的是近裁剪的距离
 * far  调整的是远裁剪的距离
 * 
 * 也是可视空间的概念
 */
export class CameraOrthoEmulator extends CameraEmulator{
    protected readonly VS: string =
        "attribute vec4 a_Position;\n" +
        "attribute vec4 a_Color;\n" +
        "uniform mat4 u_ProjectMatrix;\n" +
        "uniform mat4 u_ViewMatrix;\n" +
        "varying vec4 v_Color;\n" +
        "void main(){\n" +
        "   v_Color = a_Color;\n" +
        "   gl_Position = u_ProjectMatrix * u_ViewMatrix * a_Position;\n" +
        "}\n";

    private u_ProjectMatrix:WebGLUniformLocation;
    private _projectMatrix:Matrix4;
    private _near:number = 0.0;
    private _far:number = 2.0;

    private _changeOp:boolean;

    protected _initShaderReference(gl: WebGL2RenderingContext | WebGLRenderingContext) {
        super._initShaderReference(gl);
        // @ts-ignore
        let program = gl.program;
        this.u_ProjectMatrix = gl.getUniformLocation(program, "u_ProjectMatrix");
    }

    protected _initShaderValue(gl: WebGL2RenderingContext | WebGLRenderingContext): void {
        super._initShaderValue(gl);
        this._projectMatrix ||= new Matrix4();
        let addition = 1.0;
        this._projectMatrix.setOrtho(-addition,addition,-addition,addition,this._near,this._far);
        gl.uniformMatrix4fv(this.u_ProjectMatrix, false, this._projectMatrix.elements);
    }

    onKeyDown(gl: WebGL2RenderingContext | WebGLRenderingContext, ev: KeyboardEvent) {
        if(ev.keyCode == Keyboard.C){
            this._changeOp = !this._changeOp;
        }
        if(this._changeOp){
            
           super.onKeyDown(gl, ev);
           return; 
        }
        if (ev.keyCode == Keyboard.A) {
            this._far -= 0.01;
        }
        if (ev.keyCode == Keyboard.D) {
            this._far += 0.01;

        }
        if (ev.keyCode == Keyboard.W) {
            this._near += 0.01;
        }
        if (ev.keyCode == Keyboard.S) {
            this._near -= 0.01;
        }

        console.log(`当前的数据 near:${this._near}  far:${this._far}`);
        this._initShaderValue(gl);
        this.draw(gl);
    }


}